package readers;

import org.apache.log4j.Logger;
import org.supercsv.io.CsvListReader;

import java.util.List;
import java.io.IOException;

public class CSVRowReader implements RowReader {
    private final CsvListReader reader;
    private String[] headers;

    public CSVRowReader(CsvListReader reader, boolean isWithHeader) throws IOException {
        this.reader = reader;
        if(isWithHeader) {
            headers = reader.getCSVHeader(true);
        }
    }


    public List<String> read() throws Exception {
        return reader.read();
    }

    public void printHeader(Logger log) {
        if(headers == null) {
            log.debug("read file without header information");
            return;
        }
        
        int i = 0;
        for(String header : headers) {
            log.debug("header field ["+ (i++) + "]: " + header);
        }
    }

    public void close() throws Exception {
        reader.close();
    }
}